var longitude = "",
  latitude = "",
  util = require("../../utils/helper.js");
var INPUT_DATA = 'submit_order_input_data';
// var PICKER_ADDRESS = 'submit_order_picker_address';
var PICKER_ADDRESS = "picker_address";
Page({
  data: {
    total_price: 0,
    address: null,
    express_price: 0,
    content: "",
    offline: 0,
    express_price_1: 0,
    name: "",
    mobile: "",
    integral_radio: 1,
    new_total_price: 0,
    show_card: !1,
    payment: -1,
    show_payment: !1,
    distance: -1
  },
  onLoad: function(t) {
    getApp().page.onLoad(this, t);
    var e = util.formatData(new Date());
    wx.removeStorageSync(INPUT_DATA), this.setData({
      options: t,
      time: e
    });
    this.themeColor();
  },
  themeColor: function() {
    var cl = wx.getStorageSync('_navigation_bar_color').themeColor;
    var cl_b = !cl || cl == '#ffffff' ? '#ff4544' : cl;
    var color_rgb = this.hexToRgb(cl_b);
    var back_g = 'rgba(' + color_rgb.r + ',' + color_rgb.g + ',' + color_rgb.b + ',.2)'
    this.setData({
      all_color: cl ? cl : '#ffffff',
      back_g: back_g
      // back_end: back_end
    });
    wx.setNavigationBarColor({
      frontColor: cl != '#ffffff' ? '#ffffff' : '#000000',
      backgroundColor: cl,
      animation: {
        duration: 0,
        timingFunc: 'easeIn'
      }
    })
  },
  hexToRgb: function(hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
      r: parseInt(result[1], 16),
      g: parseInt(result[2], 16),
      b: parseInt(result[3], 16)
    } : null;
  },
  bindkeyinput: function(t) {
    this.setData({
      content: t.detail.value
    });
  },
  KeyName: function(t) {
    this.setData({
      name: t.detail.value
    });
  },
  KeyMobile: function(t) {
    this.setData({
      mobile: t.detail.value
    });
  },
  location: function () {
    var t = this;
    wx.openLocation({
      latitude: parseFloat(t.data.shop.latitude),
      longitude: parseFloat(t.data.shop.longitude),
      address: t.data.shop.address,
      name: t.data.shop.name
    });
  },
  getOffline: function(t) {
    var e = this.data.express_price,that= this;
      var index = t.target.dataset.index;
      if(index == 1){
        this.setData({
          offline: 1,
          is_peisong: 0,
          express_price: 0,
          express_price_1: e> 0 ? e : that.data.express_price_1
        })
      }else if(index == 2){
        this.setData({
          offline: 1,
          is_peisong: 1,
          express_price: 0,
          express_price_1: e> 0 ? e : that.data.express_price_1
        })
      }else{
        this.setData({
          offline: 0,
          is_peisong: 0,
          express_price: e> 0 ? e : that.data.express_price_1
        })
      }
     this.getPrice();
  },
  dingwei: function() {
    var e = this;
    getApp().getauth({
      content: "需要获取您的地理位置授权，请到小程序设置中打开授权",
      author: "scope.userLocation",
      success: function(t) {
        t && (t.authSetting["scope.userLocation"] ? wx.chooseLocation({
          success: function(t) {
            longitude = t.longitude, latitude = t.latitude, e.setData({
              location: t.address
            });
          }
        }) : wx.showToast({
          title: "您取消了授权",
          image: "/images/icon-warning.png"
        }));
      }
    });
  },
  orderSubmit: function(t) {
    var e = this,
      a = e.data.offline,
      is_peisong = e.data.is_peisong,
      peisong_price = e.data.peisong_price,
      i = {};
    if (0 == a) {
      if (!e.data.address || !e.data.address.id) return void wx.showToast({
        title: "请选择收货地址",
        image: "/images/icon-warning.png"
      });
      i.address_id = e.data.address.id;
    } else {
      i.shop_id = e.data.shop.id;
      if(is_peisong == 1){
        if (!e.data.address || !e.data.address.id) return void wx.showToast({
          title: "请选择收货地址",
          image: "/images/icon-warning.png"
        });
        i.address_id = e.data.address.id;
      }else{
        if (i.address_name = e.data.name, i.address_mobile = e.data.mobile, !e.data.shop.id) return void wx.showModal({
          title: "警告",
          content: "请选择社区",
          showCancel: !1
        });
  
        if (!i.address_name || null == i.address_name) return void wx.showToast({
          title: "请填写联系人",
          image: "/images/icon-warning.png"
        });
        if (!i.address_mobile || null == i.address_mobile) return void wx.showToast({
          title: "请填写联系电话",
          image: "/images/icon-warning.png"
        });
      }
   
    }
    if (i.offline = a,i.is_peisong = is_peisong, i.peisong_price = peisong_price, -1 == e.data.payment) return e.setData({
      show_payment: !0
    }), !1;
    e.data.cart_id_list && (i.cart_id_list = JSON.stringify(e.data.cart_id_list)), e.data.goods_info && (i.goods_info = JSON.stringify(e.data.goods_info)),
      e.data.picker_coupon && (i.user_coupon_id = e.data.picker_coupon.user_coupon_id),
      e.data.content && (i.content = e.data.content), 1 == e.data.integral_radio ? i.use_integral = 1 : i.use_integral = 2,
      i.payment = e.data.payment, i.formId = t.detail.formId, e.order_submit(i, "ms");
  },
  onReady: function(t) {
    getApp().page.onReady(this);
  },
  onShow: function(t) {
    getApp().page.onShow(this);
    var e = this,
      a = wx.getStorageSync(PICKER_ADDRESS);
    a && (e.setData({
        address: a,
        name: a.name,
        mobile: a.mobile
      }), wx.removeStorageSync(PICKER_ADDRESS), e.getInputData());
      var s = wx.getStorageSync("picker_order");
      if (s ) {
        var shop = s;
        if (s.peisong_type) {
          shop.peisong_type = s.peisong_type;
        }
        var distance = s.distance;
        var peisong_price = s.peisong_price;
         e.setData({
           shop: shop,
           distance: distance,
           peisong_price: peisong_price - 0,
           send_type: s.peisong_type ? s.peisong_type : e.data.send_type
        })
        wx.removeStorageSync('picker_order')
        this.getPrice();
      }else {
        e.getOrderData(e.data.options);
      }
      
  },
  getOrderData: function(t) {
    var o = this,
      e = "";
    o.data.address && o.data.address.id && (e = o.data.address.id), t.goods_info && (wx.showLoading({
      title: "正在加载",
      mask: !0
    }), getApp().request({
      url: getApp().api.miaosha.submit_preview,
      data: {
        goods_info: t.goods_info,
        address_id: e,
        longitude: longitude,
        latitude: latitude
      },
      success: function(t) {
        if (wx.hideLoading(), 0 == t.code) {
          var e = t.data.shop,
            a = {};
          1 == e && (a = e[0]);
          var i = wx.getStorageSync(INPUT_DATA);
          i || (i = {
              address: t.data.address,
              name: t.data.address ? t.data.address.name : "",
              mobile: t.data.address ? t.data.address.mobile : "",
              shop: a
            }, 0 < t.data.pay_type_list.length && (i.payment = t.data.pay_type_list[0].payment,
              1 < t.data.pay_type_list.length && (i.payment = -1))), i.total_price = t.data.total_price,
            i.level_price = t.data.level_price, i.is_level = t.data.is_level, i.goods_list = t.data.list,
            i.goods_info = t.data.goods_info, i.express_price = parseFloat(t.data.express_price),
            i.coupon_list = t.data.coupon_list, i.shop = o.data.shop ?o.data.shop: t.data.shop, 
            i.send_type = o.data.send_type ? o.data.send_type : (typeof(t.data.send_type) == 'string' ? JSON.parse(t.data.send_type): t.data.send_type),
            i.level = t.data.level, i.integral = t.data.integral, i.new_total_price = t.data.level_price,
            i.is_payment = t.data.is_payment, i.is_coupon = t.data.list[0].coupon, i.is_discount = t.data.list[0].is_discount,
            i.is_area = t.data.is_area, i.pay_type_list = t.data.pay_type_list, i.offline=t.data.offline, i.is_peisong=t.data.is_peisong, 
            i.peisong_price = parseFloat(o.data.peisong_price ? o.data.peisong_price : t.data.peisong_price), o.setData(i),
            o.getInputData(), o.getPrice();
        }

        1 == t.code && wx.showModal({
          title: "提示",
          content: t.msg,
          showCancel: !1,
          confirmText: "返回",
          success: function(t) {
            t.confirm && (1 == getCurrentPages().length ? wx.redirectTo({
              url: "/pages/index/index"
            }) : wx.navigateBack({
              delta: 1
            }));
          }
        });
      },
      complete: function () {
        if (o.data.shop && o.data.distance < 0) {
          wx.getLocation({
            success: function (res) {
              var dis = o.distance(res.latitude, res.longitude, o.data.shop.latitude, o.data.shop.longitude)
              o.setData({
                distance: dis
              })
            }
          });
        }
      } 
    }));
  },
  distance: function (la1, lo1, la2, lo2) {

    var La1 = la1 * Math.PI / 180.0;

    var La2 = la2 * Math.PI / 180.0;

    var La3 = La1 - La2;

    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;

    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));

    s = s * 6378.137; //地球半径

    s = Math.round(s * 10000) / 10000;
    return s.toFixed(2) + "km";
  },
  copyText: function(t) {
    var e = t.currentTarget.dataset.text;
    e && wx.setClipboardData({
      data: e,
      success: function() {
        self.showToast({
          title: "已复制内容"
        });
      },
      fail: function() {
        self.showToast({
          title: "复制失败",
          image: "/images/icon-warning.png"
        });
      }
    });
  },
  showCouponPicker: function() {
    var t = this;
    t.getInputData(), t.data.coupon_list && 0 < t.data.coupon_list.length && t.setData({
      show_coupon_picker: !0
    });
  },
  pickCoupon: function(t) {
    var e = wx.getStorageSync(INPUT_DATA);
    wx.removeStorageSync(INPUT_DATA);
    var a = t.currentTarget.dataset.index;
    e.show_coupon_picker = !1, e.picker_coupon = "-1" != a && -1 != a && this.data.coupon_list[a],
      this.setData(e), this.getPrice();
  },
  numSub: function(t, e, a) {
    return 100;
  },
  showShop: function(t) {
    var e = this;
    e.getInputData(), e.dingwei(), e.data.shop_list && 1 <= e.data.shop_list.length && e.setData({
      show_shop: !0
    });
  },
  pickShop: function(t) {
    var e = t.currentTarget.dataset.index,
      a = wx.getStorageSync(INPUT_DATA);
    wx.removeStorageSync(INPUT_DATA), a.show_shop = !1, a.shop = "-1" != e && -1 != e && this.data.shop_list[e],
      this.setData(a), this.getPrice();
  },
  integralSwitchChange: function(t) {
    0 != t.detail.value ? this.setData({
      integral_radio: 1
    }) : this.setData({
      integral_radio: 2
    }), this.getPrice();
  },
  integration: function(t) {
    var e = this.data.integral.integration;
    wx.showModal({
      title: "积分使用规则",
      content: e,
      showCancel: !1,
      confirmText: "我知道了",
      confirmColor: "#ff4544",
      success: function(t) {
        t.confirm;
      }
    });
  },
  getPrice: function() {
    var t = this,
      e = (t.data.total_price, parseFloat(t.data.level_price)),
      a = t.data.express_price,
      i = t.data.picker_coupon,
      o = t.data.integral,
      s = t.data.integral_radio,
      n = (t.data.level,
        t.data.is_level, t.data.offline),
        offline = t.data.offline,
        is_peisong = t.data.is_peisong,
        peisong_price = t.data.peisong_price ? t.data.peisong_price : 0;
    i && (e -= i.sub_price), o && 1 == s && (e -= parseFloat(o.forehead)), e <= .01 && (e = .01),
      0 == n && (console.log(a), e += a, console.log(e)), e = parseFloat(e), t.setData({
        new_total_price: e.toFixed(2)
      });
      if(offline==1 && is_peisong==1){
         e += peisong_price, e = parseFloat(e);
         t.setData({
          new_total_price: e.toFixed(2)
        })
      }
  },
  cardDel: function() {
    this.setData({
      show_card: !1
    }), wx.redirectTo({
      url: "/pages/order/order?status=1"
    });
  },
  cardTo: function() {
    this.setData({
      show_card: !1
    }), wx.redirectTo({
      url: "/pages/card/card"
    });
  },
  formInput: function(t) {
    var e = t.currentTarget.dataset.index,
      a = this.data.form,
      i = a.list;
    i[e].default = t.detail.value, a.list = i, this.setData({
      form: a
    });
  },
  selectForm: function(t) {
    var e = t.currentTarget.dataset.index,
      a = t.currentTarget.dataset.k,
      i = this.data.form,
      o = i.list;
    if ("radio" == o[e].type) {
      var s = o[e].default_list;
      for (var n in s) n == a ? s[a].is_selected = 1 : s[n].is_selected = 0;
      o[e].default_list = s;
    }
    "checkbox" == o[e].type && (1 == (s = o[e].default_list)[a].is_selected ? s[a].is_selected = 0 : s[a].is_selected = 1,
      o[e].default_list = s);
    i.list = o, this.setData({
      form: i
    });
  },
  showPayment: function() {
    this.setData({
      show_payment: !0
    });
  },
  payPicker: function(t) {
    var e = t.currentTarget.dataset.index;
    this.setData({
      payment: e,
      show_payment: !1
    });
  },
  payClose: function() {
    this.setData({
      show_payment: !1
    });
  },
  getInputData: function() {
    var t = this,
      e = {
        address: t.data.address,
        name: t.data.name,
        mobile: t.data.mobile,
        content: t.data.content,
        payment: t.data.payment,
        shop: t.data.shop
      };
    wx.setStorageSync(INPUT_DATA, e);
  },
  onHide: function(t) {
    getApp().page.onHide(this), this.getInputData();
  },
  onUnload: function(t) {
    getApp().page.onUnload(this), wx.removeStorageSync(INPUT_DATA);
  }
});